package vcp.client.GUI.mvc.view.addons;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import vcp.client.statistics.ActivityStatistics;
import vcp.client.statistics.ActivityStatistics.ActivityDataHolder;

public class ActivityChartBarPanel extends JPanel
{

	/**
	 * 
	 */
	private static final long serialVersionUID = - 8288850693332677498L;
	private JTextField tfMedianUsedOrders;
	private JTextField tfMedianCanceledOrders;
	private JTextField tfDeviationUsedOrders;
	private JTextField tfDeviationCanceledOrders;

	private ChartPanel chartPanel;

	public ActivityChartBarPanel ( ActivityStatistics statistics )
	{
		this.initChart(statistics);
		this.setupLayout();
		this.initAdditionalParams(statistics);
	}

	private void initAdditionalParams ( ActivityStatistics statistics )
	{
		this.tfMedianUsedOrders.setText(String.valueOf(statistics.getMedianUsed()));
		this.tfMedianCanceledOrders.setText(String.valueOf(statistics.getMedianCanceled()));

		String deviationCanceled = String.format("%.2f", statistics.getDeviationCanceled());
		String deviationUsed = String.format("%.2f", statistics.getDeviationUsed());
		this.tfDeviationCanceledOrders.setText(deviationCanceled);
		this.tfDeviationUsedOrders.setText(deviationUsed);
	}

	private void initChart ( ActivityStatistics statistics )
	{
		DefaultCategoryDataset dataSet = this.createCategoryDataSet(statistics);
		JFreeChart chart = this.createChart(dataSet, "Activity", "Sites", "Values");
		this.chartPanel = new ChartPanel(chart);
		this.chartPanel.setMaximumDrawHeight(350);
		this.chartPanel.setMaximumDrawWidth(700);
	}

	private JFreeChart createChart ( DefaultCategoryDataset dataSet, String title, String xTitle, String yTitle )
	{
		JFreeChart chart = ChartFactory.createBarChart3D(title, xTitle, yTitle, dataSet, PlotOrientation.VERTICAL, true, true, false);
		return this.customizeChart(chart);
	}

	private JFreeChart customizeChart ( JFreeChart chart )
	{
		chart.setBackgroundPaint(Color.LIGHT_GRAY);

		final CategoryPlot plot = chart.getCategoryPlot();
		plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
		plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
		final CategoryItemRenderer renderer1 = plot.getRenderer();
		renderer1.setSeriesPaint(0, Color.red);
		renderer1.setSeriesPaint(1, Color.yellow);

		plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
		return chart;
	}

	private DefaultCategoryDataset createCategoryDataSet ( ActivityStatistics statistics )
	{
		DefaultCategoryDataset dataSet = new DefaultCategoryDataset();

		String usedSeries = statistics.getUsedSeries();
		String canceledSeries = statistics.getCanceledSeries();

		for ( ActivityDataHolder dataHolder : statistics.getActivityStatistics() )
		{
			dataSet.addValue(dataHolder.getNumOfUsed(), usedSeries, dataHolder.getSiteName());
			dataSet.addValue(dataHolder.getNumOfCanceled(), canceledSeries, dataHolder.getSiteName());
		}

		return dataSet;
	}

	private void setupLayout ()
	{
		this.setPreferredSize(new Dimension(908, 704));
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		this.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));

		JPanel panel_17 = new JPanel();
		panel.add(panel_17, BorderLayout.SOUTH);

		this.chartPanel.setPreferredSize(new Dimension(500, 150));
		panel.add(this.chartPanel, BorderLayout.CENTER);

		JPanel panel_1 = new JPanel();
		panel_1.setPreferredSize(new Dimension(10, 90));
		this.add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new GridLayout(3, 0, 0, 0));

		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 6, 0, 0));

		JPanel panel_5 = new JPanel();
		panel_2.add(panel_5);

		JPanel panel_6 = new JPanel();
		panel_2.add(panel_6);

		JLabel lblNewLabel = new JLabel("Used Orders");
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		panel_6.add(lblNewLabel);

		JPanel panel_7 = new JPanel();
		panel_2.add(panel_7);

		JLabel lblNewLabel_1 = new JLabel("Canceled Orders");
		lblNewLabel_1.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		panel_7.add(lblNewLabel_1);

		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 6, 0, 0));

		JPanel panel_9 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_9.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panel_3.add(panel_9);

		JLabel lblMedian = new JLabel("Median");
		lblMedian.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		panel_9.add(lblMedian);

		JPanel panel_10 = new JPanel();
		FlowLayout flowLayout_5 = (FlowLayout) panel_10.getLayout();
		flowLayout_5.setVgap(2);
		panel_3.add(panel_10);

		this.tfMedianUsedOrders = new JTextField();
		this.tfMedianUsedOrders.setEnabled(false);
		panel_10.add(this.tfMedianUsedOrders);
		this.tfMedianUsedOrders.setColumns(10);

		JPanel panel_11 = new JPanel();
		FlowLayout flowLayout_6 = (FlowLayout) panel_11.getLayout();
		flowLayout_6.setVgap(2);
		panel_3.add(panel_11);

		this.tfMedianCanceledOrders = new JTextField();
		this.tfMedianCanceledOrders.setEnabled(false);
		panel_11.add(this.tfMedianCanceledOrders);
		this.tfMedianCanceledOrders.setColumns(10);

		JPanel panel_4 = new JPanel();
		panel_1.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 6, 0, 0));

		JPanel panel_13 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_13.getLayout();
		flowLayout_1.setAlignment(FlowLayout.RIGHT);
		panel_4.add(panel_13);

		JLabel lblDi = new JLabel("Deviation");
		lblDi.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		panel_13.add(lblDi);

		JPanel panel_14 = new JPanel();
		FlowLayout flowLayout_8 = (FlowLayout) panel_14.getLayout();
		flowLayout_8.setVgap(2);
		panel_4.add(panel_14);

		this.tfDeviationUsedOrders = new JTextField();
		this.tfDeviationUsedOrders.setEnabled(false);
		panel_14.add(this.tfDeviationUsedOrders);
		this.tfDeviationUsedOrders.setColumns(10);

		JPanel panel_15 = new JPanel();
		FlowLayout flowLayout_9 = (FlowLayout) panel_15.getLayout();
		flowLayout_9.setVgap(2);
		panel_4.add(panel_15);

		this.tfDeviationCanceledOrders = new JTextField();
		this.tfDeviationCanceledOrders.setEnabled(false);
		panel_15.add(this.tfDeviationCanceledOrders);
		this.tfDeviationCanceledOrders.setColumns(10);
	}
}
